package com.zk.zkapidemo.javaapi;

import org.apache.zookeeper.*;


import java.io.IOException;
import java.util.concurrent.CountDownLatch;

public class CreateSession {
    private static CountDownLatch countDownLatch = new CountDownLatch(1);
    private final static String CONNECTSTRING="192.168.3.151:2181";


    public static void main(String[] args)throws IOException, InterruptedException {

//        ZooKeeper zooKeeper = new ZooKeeper(CONNECTSTRING,5000,watchedEvent -> new Watcher(){
//            @Override
//            public void process(WatchedEvent watchedEvent) {
//                if(watchedEvent.getState() == Event.KeeperState.SyncConnected){
//                    countDownLatch.countDown();
//                    System.out.println(watchedEvent.getState());
//                }
//            }
//        });

        ZooKeeper zooKeeper = new ZooKeeper(CONNECTSTRING, 5000, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                //如果当前的连接状态是连接成功的，那么通过计数器去控制
                if(watchedEvent.getState()==Event.KeeperState.SyncConnected){
                    countDownLatch.countDown();
                    System.out.println(watchedEvent.getState());
                }
            }
        });

//        ZooKeeper zooKeeper=new ZooKeeper(CONNECTSTRING, 5000, new Watcher() {
//            public void process(WatchedEvent watchedEvent) {
//                //如果当前的连接状态是连接成功的，那么通过计数器去控制
//                if(watchedEvent.getState()==Event.KeeperState.SyncConnected){
//                    countDownLatch.countDown();
//                    System.out.println(watchedEvent.getState());
//                }
//            }
//        });
        countDownLatch.await();
        System.out.println(zooKeeper.getState());



        //创建节点
//        String result=zooKeeper.create("/node1","123".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
//        zooKeeper.getData("/node1",new ApiOperatorDemo(),stat); //增加一个
//        System.out.println("创建成功："+result);

        //修改数据
        //zookeeper.getData("/node1",new ApiOperatorDemo(),stat);

//        zookeeper.setData("/node1","deer2".getBytes(),-1);
//        Thread.sleep(2000);


        //删除节点
//        zookeeper.getData("/node1",new ApiOperatorDemo(),stat);
//        zookeeper.delete("/node1",-1);
//        Thread.sleep(2000);

        // 创建节点和子节点
//        String path="/node11";
//
//        zookeeper.create(path,"123".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
//        TimeUnit.SECONDS.sleep(1);
//
//        Stat stat=zookeeper.exists(path+"/node1",true);
//        if(stat==null){//表示节点不存在
//            zookeeper.create(path+"/node1","123".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
//            TimeUnit.SECONDS.sleep(1);
//        }
//        //修改子路径
//        zookeeper.setData(path+"/node1","deer".getBytes(),-1);
//        TimeUnit.SECONDS.sleep(1);


        //获取指定节点下的子节点
//        List<String> childrens=zookeeper.getChildren("/node11",true);
//        System.out.println(childrens);


    }
}
